package com.bbzn.device.client.service.emq.alarm;

import com.alibaba.fastjson.JSONObject;
import com.bbzn.device.client.constant.TitleConstant;
import com.bbzn.device.client.dataobject.Switch;
import com.bbzn.device.client.dto.ct.data.EmqAlarmDTO;
import com.bbzn.device.client.service.EmqStrategy;
import com.bbzn.device.client.service.SwitchService;
import com.bbzn.device.client.service.emq.alarm.EmqBaseAlarm;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author wulongbo
 * @Date 2021/12/3 16:33
 * @Version 1.0
 */
@Component
public class EmqOffWearAlarmStrategy extends EmqBaseAlarm implements EmqStrategy {

    @Resource
    private SwitchService switchService;

    @Override
    public String emqAction(String payLoad) {
        EmqAlarmDTO emqAlarm = new EmqAlarmDTO();
        List<String> list = new ArrayList<>();
        list.add("imei");
        list.add("state");
        list.add("type");
        list.add("lat");
        list.add("lon");
        list.add("location");

        /*Long userId = this.checkPayload(list, payLoad);
        if (userId == null) {
            return "";
        }*/

        JSONObject jsonObject = JSONObject.parseObject(payLoad);
        // 0：脱落 / 1：佩戴
        Integer state = jsonObject.getInteger("state");
        String cnInfo = null;
        String enInfo = null;
        int alarmType = 0;
        if (state == 0) {  // 0：脱落 / 1：佩戴
            cnInfo = "异常消息: 设备脱落";
            enInfo = "abnormal message: device falling off";
            alarmType = 5;
        } else {
            cnInfo = "正常消息: 设备佩戴";
            enInfo = "normal message: device wearing";
            alarmType = 6;
        }
        // 定位类型【1：GPS / 2：传感器 / 4：缓存定位 / 5：wifi定位 / 6：基站定位】
        Integer type = jsonObject.getInteger("type");
        if (null == type || "".equals(type)){
            type = 5;
        }
        String lat = jsonObject.getString("lat");
        String lon = jsonObject.getString("lon");
        String location = jsonObject.getString("location");

        emqAlarm.setImei(jsonObject.getString("imei"));
        emqAlarm.setAlarmType(alarmType);
        emqAlarm.setTitle(state == 0 ? TitleConstant.DROP_ALARM : TitleConstant.WEAR_ALARM);
        emqAlarm.setEnTitle(state == 0 ? TitleConstant.DROP_ALARM_EN : TitleConstant.WEAR_ALARM_EN);

        String cnSign = MessageFormat.format("[穿戴报警]-{0}", cnInfo);
        String enSign = MessageFormat.format("[wear alarm] -{0}", enInfo);
        emqAlarm.setCnSign(cnSign);
        emqAlarm.setEnSign(enSign);
        emqAlarm.setLat(lat);
        emqAlarm.setLon(lon);
        emqAlarm.setLocation(location);
        emqAlarm.setType(type);

        // web端穿戴/短信/邮箱开关检查
        Switch need = new Switch();
        need.setImei(jsonObject.getString("imei"));
        List<Boolean> checkResult = switchService.checkSwitchByOffWear(need, state);
        emqAlarm.setBooleanList(checkResult);
        this.alarmRun(emqAlarm);
        return "";
    }
}
